package com.future.hedongjun.mine.order;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.utils.ImageUtils;
import com.future.baselib.view.ActionSheetDialog;
import com.future.hedongjun.R;
import com.future.hedongjun.common.PriceHelper;
import com.future.hedongjun.mall.bean.OrderDetail;
import com.future.hedongjun.mine.adapter.RefundDetailAdapter;
import com.future.hedongjun.mine.adapter.SelectImageAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefundActivity extends BaseActivity implements View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {

    public static final int TYPE_REFUND = 1;
    public static final int TYPE_REFUND_DETAIL = 2;
    @BindView(R.id.recyclerView_goods)
    RecyclerView recyclerViewGoods;
    @BindView(R.id.tv_refund_reason)
    TextView tvRefundReason;
    @BindView(R.id.iv_right_1)
    ImageView ivRight1;
    @BindView(R.id.layout_refund_reason)
    FrameLayout layoutRefundReason;
    @BindView(R.id.tv_tip_refund_price)
    TextView tvTipRefundPrice;
    @BindView(R.id.tv_refund_price)
    TextView tvRefundPrice;
    @BindView(R.id.tv_tip_pingzheng)
    TextView tvTipPingzheng;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.bottom_btn)
    Button bottomBtn;
    private int type;
    private OrderDetail data;
    private SelectImageAdapter adapter;
    private View footView;
    private List<LocalMedia> selectList;
    private ArrayList<String> items;
    private OptionsPickerView<String> pvOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        ButterKnife.bind(this);
        if (type == TYPE_REFUND) {
            initViewRequest();
        } else if (type == TYPE_REFUND_DETAIL) {
            initViewDetail();
        }
    }

    private void initViewDetail() {
        ivRight1.setVisibility(View.GONE);
        recyclerViewGoods.setVisibility(View.GONE);
        setTitle("退款详情");
    }

    private void initViewRequest() {
        setTitle("申请退款");
        recyclerViewGoods.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewGoods.setAdapter(new RefundDetailAdapter(this.data.goodsBeanList));
        tvRefundPrice.setText(PriceHelper.priceFormat(data.amount));
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new SelectImageAdapter(R.layout.item_image, null);
        recyclerView.setAdapter(adapter);
        adapter.setFooterViewAsFlow(true);
        footView = getLayoutInflater().inflate(R.layout.layout_take_photo, null);
        footView.setOnClickListener(this);
        adapter.setFooterView(footView);
        adapter.setOnItemChildClickListener(this);
        bottomBtn.setText("提交");
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        type = extras.getInt("type");
        data = (OrderDetail) extras.getSerializable("data");
    }

    @OnClick({R.id.layout_refund_reason, R.id.bottom_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_refund_reason:
                if (type == TYPE_REFUND) {
                    //返回的分别是三个级别的选中位置
                    if (pvOptions == null) {
                        pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                            @Override
                            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                                //返回的分别是三个级别的选中位置
                                tvRefundReason.setText(items.get(options1));
                            }
                        }).build();
                        items = new ArrayList<>();
                        items.add("不想要了");
                        items.add("七天无理由");
                        items.add("商品破损");
                        pvOptions.setPicker(items);
                    }
                    pvOptions.show();
                }
                break;
            case R.id.bottom_btn:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        ActionSheetDialog dialog = new ActionSheetDialog(this).builder()
                .addSheetItem("拍照", ActionSheetDialog.SheetItemColor.BLACK, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        PictureSelector.create(RefundActivity.this)
                                .openCamera(PictureMimeType.ofImage())
                                .forResult(PictureConfig.CAMERA);
                    }
                })
                .addSheetItem("相册", ActionSheetDialog.SheetItemColor.BLACK, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        PictureSelector.create(RefundActivity.this)
                                .openGallery(PictureMimeType.ofImage())
                                .maxSelectNum(3)
                                .isCamera(false)
                                .selectionMedia(selectList)
                                .forResult(PictureConfig.CHOOSE_REQUEST);
                    }
                })
                .setCancelable(true)
                .setTitle("选择照片来源");
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    List<String> images = new ArrayList<>();
                    for (int i = 0; i < selectList.size(); i++) {
                        images.add(selectList.get(i).getPath());
                    }
                    if (selectList.size() == 3) {
                        adapter.removeFooterView(footView);
                    }
                    adapter.setNewData(images);
                    break;
                case PictureConfig.CAMERA:
                    List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                    selectList.add(localMedia.get(0));
                    adapter.addData(localMedia.get(0).getPath());
                    if (selectList.size() == 3) {
                        adapter.removeFooterView(footView);
                    }
                    break;
            }
        }
    }

    class File2BASE64Task extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings) {

            ArrayList<String> imgs = new ArrayList<>();
            for (int i = 0; i < strings.length; i++) {
                imgs.add(strings[i]);
            }
            String imgs1 = ImageUtils.getImgs(imgs);
            return imgs1;
        }

        @Override
        protected void onPostExecute(String s) {

        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        adapter.remove(position);
        if (adapter.getFooterViewsCount() == 0) {
            adapter.addFooterView(footView);
        }
    }
}
